package microcontroler;

import microcontroler.SerialPortConnector.Light;

public class MicroControler {
	private boolean lightRoom = false;
	private boolean lightKitchen = false;
	private boolean lightBathroom = false;
	private int heatingLevel = 0;
	private SerialPortConnector spc;
	private boolean alarm = false;
	
	
	public MicroControler(){
		spc = new SerialPortConnector("/dev/ttyUSB0", 9600);
		Thread alarmThread = new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){
					
					while(spc.getAlarm() == false){
						if(alarm == true){
							
							unsetAlarm();
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					alarm = true;
					System.out.println("ALARM");
				}
			}
		});
		//alarmThread.start();
	}
	
	public void unsetAlarm(){
		alarm=false;
	}
	
	public boolean getAlarmStatus(){
		return spc.getAlarm();
	}
	
	private void sendToHardware(int i){
		if(i < 5 && i >= 0){
			spc.setLine(i);
		}else if(i == DIODE_ROOM){
			spc.toogleLight(Light.ROOM_LIGHT);
		}else if(i == DIODE_KITCHEN){
			spc.toogleLight(Light.KITCHEN_LIGHT);
		}else if(i == DIODE_BATHROOM){
			spc.toogleLight(Light.BATHROOM_LIGHT);
		}
	}
	
	private void changeLightInRoom(){
		sendToHardware(DIODE_ROOM);
		lightRoom ^= true;
	}
	
	private void changeLightInBathroom(){
		sendToHardware(DIODE_BATHROOM);
		lightBathroom ^= true;
	}
	
	private void changeLightInKitchen(){
		sendToHardware(DIODE_KITCHEN);
		lightKitchen ^= true;
	}
	
	public void turnLightOnInRoom(){
		if(!lightRoom){
			changeLightInRoom();
		}
	}
	
	public void turnLightOffInRoom(){
		if(lightRoom){
			changeLightInRoom();
		}
	}
	
	public void turnLightOnInBathroom(){
		if(!lightBathroom){
			changeLightInBathroom();
		}
	}
	
	public void turnLightOffInBathroom(){
		if(lightBathroom){
			changeLightInBathroom();
		}
	}
	
	public void turnLightOnInKitchen(){
		if(!lightKitchen){
			changeLightInKitchen();
		}
	}
	
	public void turnLightOffInKitchen(){
		if(lightKitchen){
			changeLightInKitchen();
		}
	}
	
	public void setHeatingTemperature(int temp){
		switch(temp){
		case 18:
			sendToHardware(DIODES_HEATING_LEVEL_1);
			heatingLevel = 1;
			break;
		case 20:
			sendToHardware(DIODES_HEATING_LEVEL_2);
			heatingLevel = 2;
			break;
		case 22:
			sendToHardware(DIODES_HEATING_LEVEL_3);
			heatingLevel = 3;
			break;
		case 24:
			sendToHardware(DIODES_HEATING_LEVEL_4);
			heatingLevel = 4;
			break;
		default:
			sendToHardware(DIODES_HEATING_LEVEL_0);
			heatingLevel = 0;
			break;
		}	
	}
	
	public State getState(){
		return new State(lightRoom, lightBathroom, lightKitchen, heatingLevel);
	}
	
	private static final int DIODE_ROOM = 5;
	private static final int DIODE_BATHROOM = 7;
	private static final int DIODE_KITCHEN = 6;
	private static final int DIODES_HEATING_LEVEL_0 = 0;
	private static final int DIODES_HEATING_LEVEL_1 = 1;
	private static final int DIODES_HEATING_LEVEL_2 = 2;
	private static final int DIODES_HEATING_LEVEL_3 = 3;
	private static final int DIODES_HEATING_LEVEL_4 = 4;
	
	public static void main(String [] args) throws InterruptedException{
		MicroControler mc = new MicroControler();
		while(true){
		mc.turnLightOnInBathroom();
		Thread.sleep(1000);
		mc.turnLightOnInKitchen();
		Thread.sleep(1000);
		mc.turnLightOnInRoom();
		Thread.sleep(1000);
		mc.turnLightOffInBathroom();
		Thread.sleep(1000);
		mc.turnLightOffInKitchen();
		Thread.sleep(1000);
		mc.turnLightOffInRoom();
		Thread.sleep(1000);
		mc.setHeatingTemperature(18);
		Thread.sleep(1000);
		mc.setHeatingTemperature(20);
		Thread.sleep(1000);
		mc.setHeatingTemperature(22);
		Thread.sleep(1000);
		mc.setHeatingTemperature(24);
		Thread.sleep(1000);
		mc.setHeatingTemperature(0);
		Thread.sleep(1000);
		}
	}
}
